//
//  CAMainTabbarController.m
//  cascade-realm
//
//  Created by QSC on 20/5/24.
//

#import "CAMainTabbarController.h"
#import "CAHomeController.h"
#import "CAVideoController.h"
#import "CAForumController.h"
#import "CAMineController.h"
#import "CAPlusButton.h"
@interface CAMainTabbarController ()

@end

@implementation CAMainTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
 
    
    [CAPlusButton registerPlusButton];
 
    [self customizeInterface];
}

- (instancetype)init {
   if (!(self = [super init])) {
       return nil;
   }
    UIEdgeInsets imageInsets = UIEdgeInsetsZero;
    UIOffset titlePositionAdjustment = UIOffsetMake(0, 0);
    
    if (kBottomSafeHeight == 0) {
        imageInsets = UIEdgeInsetsMake(-2, 0, 0, 0);
        titlePositionAdjustment = UIOffsetMake(0, -3);
    }
    
    
    CYLTabBarController * tabBarController = [[CYLTabBarController alloc] initWithViewControllers:self.viewControllers
                    tabBarItemsAttributes:[self tabBarItemsAttributesForTabBar]
                              imageInsets:imageInsets
                  titlePositionAdjustment:titlePositionAdjustment
                                  context:@""];
    if (self) {
        [self customizeInterface];
        self.delegate = self;
        self.navigationController.navigationBar.hidden = YES;
    }
    return (self = (CAMainTabbarController *)tabBarController);
}

- (NSArray *)viewControllers {
  
    CAHomeController * vc1 = [CAHomeController new];
    CAVideoController * vc2 = [CAVideoController new];
    CAForumController * vc3 = [CAForumController new];
    CAMineController * vc4 = [CAMineController new];

   return @[vc1,vc2,vc3, vc4];

}

- (NSArray *)tabBarItemsAttributesForTabBar {

   
    NSDictionary *homeAttributes = @{
                                      
                                      CYLTabBarItemImage : @"tabbar1_normal",
                                      CYLTabBarItemSelectedImage : @"tabbar1_selected"
                                    };
    NSDictionary *discoverAttributes = @{
                                         
                                          CYLTabBarItemImage : @"tabbar2_normal",
                                          CYLTabBarItemSelectedImage : @"tabbar2_selected"
                                        };

    NSDictionary *messageAttributes = @{
                                      
                                        CYLTabBarItemImage : @"tabbar3_normal",
                                        CYLTabBarItemSelectedImage : @"tabbar3_selected"
                                      };
    
    NSDictionary *mineAttributes = @{
                                     
                                     CYLTabBarItemImage :@"tabbar4_normal",
                                     CYLTabBarItemSelectedImage : @"tabbar4_selected"
                                    };

    


   

    return @[homeAttributes,discoverAttributes,messageAttributes,mineAttributes];
}

- (void)customizeInterface {
   
  
   
          
        [UITabBar appearance].backgroundColor = [UIColor whiteColor];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            CYLTabBarController *tabBarController = [self cyl_tabBarController];
            tabBarController.tabBar.layer.shadowColor = [UIColor blackColor].CGColor;
            tabBarController.tabBar.layer.shadowRadius = 5.0;
            tabBarController.tabBar.layer.shadowOpacity = 1;
            tabBarController.tabBar.layer.shadowOffset = CGSizeMake(0, 5);
            tabBarController.tabBar.layer.masksToBounds = NO;
            tabBarController.tabBar.clipsToBounds = NO;
        });
  
     
   
}




- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


@end




